import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:we_chat_app/const.dart';

import 'chat_cell.dart';

class ChatMessage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    return _ChatMessageState();
  }

}


class _ChatMessageState extends State<ChatMessage> {

  String content = "无信息";

  @override
  Widget build(BuildContext context) {

    return Container(child: FutureBuilder(
        future: getDatas(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if(snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: Text('loading'),);
          }
          List<ChatCell> chatCells = [];
          for(ChatCell cell in snapshot.data) {
            chatCells.add(cell);
          }
          return ListView(children: chatCells,);
        }
    ),color: contextColor,) ;
  }

  @override
  void initState() {
    getDatas();
  }

  Future<List> getDatas () async {
    Uri uri = Uri.parse("http://rap2api.taobao.org/app/mock/315396/message");
    final response = await http.get(uri);
    final responseBody = json.decode(response.body);
    return responseBody['chat_list'].map((item) => ChatCell.fromMap(item)).toList();
  }
}


